package com.demo.apparel.controller.web;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/web/auth/qr")
@Tag(name = "模拟微信二维码登录接口", description = "模拟微信二维码内容")
public class AuthController {

    // 用 Map 模拟存储二维码状态（正式环境应该放 Redis）
    private final Map<String, String> qrLoginStatus = new ConcurrentHashMap<>();

    /**
     * 生成二维码
     */
    @GetMapping("/generate")
    public Map<String, Object> generateQr() {
        String loginKey = UUID.randomUUID().toString();
        qrLoginStatus.put(loginKey, "WAITING");
        Map<String, Object> res = new HashMap<>();
        res.put("loginKey", loginKey);
        res.put("status", "WAITING");
        return res;
    }

    /**
     * 手机端确认登录
     */
    @PostMapping("/confirm")
    public Map<String, Object> confirmLogin(@RequestParam String loginKey,
                                            @RequestParam String userId) {
        if (qrLoginStatus.containsKey(loginKey)) {
            qrLoginStatus.put(loginKey, "CONFIRMED:" + userId);
            return Map.of("success", true, "msg", "扫码确认成功");
        }
        return Map.of("success", false, "msg", "无效 loginKey");
    }

    /**
     * PC 端轮询二维码状态
     */
    @GetMapping("/status")
    public Map<String, Object> checkStatus(@RequestParam String loginKey) {
        String status = qrLoginStatus.getOrDefault(loginKey, "EXPIRED");
        return Map.of("status", status);
    }
}
